package com.loeng.module.infra.controller.admin.logger;

import com.loeng.framework.apilog.core.annotation.ApiAccessLog;
import com.loeng.framework.common.pojo.CommonResult;
import com.loeng.framework.common.pojo.PageParam;
import com.loeng.framework.common.pojo.PageResult;
import com.loeng.framework.common.util.object.BeanUtils;
import com.loeng.framework.excel.util.ExcelUtils;
import com.loeng.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
import com.loeng.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogRespVO;
import com.loeng.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogSaveReqVO;
import com.loeng.module.infra.dal.dataobject.logger.ApiAccessLogDO;
import com.loeng.module.infra.service.logger.ApiAccessLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.loeng.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.loeng.framework.common.pojo.CommonResult.success;

/**
 * API 访问日志
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - API 访问日志")
@RestController
@RequestMapping("/infra/api-access-log")
@Validated
public class ApiAccessLogController {

    @Resource
    private ApiAccessLogService apiAccessLogService;

    @PostMapping("/create")
    @Operation(summary = "创建API 访问日志")
    @PreAuthorize("@ss.hasPermission('infra:api-access-log:create')")
    public CommonResult<Long> createApiAccessLog(@Valid @RequestBody ApiAccessLogSaveReqVO createReqVO) {
        return success(apiAccessLogService.createApiAccessLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新API 访问日志")
    @PreAuthorize("@ss.hasPermission('infra:api-access-log:update')")
    public CommonResult<Boolean> updateApiAccessLog(@Valid @RequestBody ApiAccessLogSaveReqVO updateReqVO) {
        apiAccessLogService.updateApiAccessLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除API 访问日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:api-access-log:delete')")
    public CommonResult<Boolean> deleteApiAccessLog(@RequestParam("id") Long id) {
        apiAccessLogService.deleteApiAccessLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得API 访问日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:api-access-log:query')")
    public CommonResult<ApiAccessLogRespVO> getApiAccessLog(@RequestParam("id") Long id) {
        ApiAccessLogDO apiAccessLog = apiAccessLogService.getApiAccessLog(id);
        return success(BeanUtils.toBean(apiAccessLog, ApiAccessLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得API 访问日志分页")
    @PreAuthorize("@ss.hasPermission('infra:api-access-log:query')")
    public CommonResult<PageResult<ApiAccessLogRespVO>> getApiAccessLogPage(@Valid ApiAccessLogPageReqVO pageReqVO) {
        PageResult<ApiAccessLogDO> pageResult = apiAccessLogService.getApiAccessLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApiAccessLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出API 访问日志 Excel")
    @PreAuthorize("@ss.hasPermission('infra:api-access-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportApiAccessLogExcel(@Valid ApiAccessLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ApiAccessLogDO> list = apiAccessLogService.getApiAccessLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "API 访问日志.xls", "数据", ApiAccessLogRespVO.class,
                        BeanUtils.toBean(list, ApiAccessLogRespVO.class));
    }

}